package com.api.fc;
//会议归档写建模的api

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.upgradetool.wscheck.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.*;
import weaver.general.BaseBean;


@Path("/fc_ks")
public class get_ks_detail {
    //    private Logger logger = LoggerFactory.getLogger(Hygdxjm.class);
    private RecordSet rs = new RecordSet();

    @GET
    @Path("/get_dts")
    @Produces(MediaType.APPLICATION_JSON)
    public String get_sy_khmc(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject errorobj = new JSONObject();
        RecordSet rs = new RecordSet();
        String requestids=request.getParameter("requestid");

        String endString="[";

        List<FcOaClass> main=new ArrayList<FcOaClass>();
        List<FcOaClass> dt1s=new ArrayList<FcOaClass>();
        List<FcOaClass> dt2s=new ArrayList<FcOaClass>();


        String sql =
                    " select * from ("+
                    " select    o1.requestid,o1.bh,dbo.convToCN(o2.status) status,o3.nodename  from formtable_main_50  o1 with(nolock),workflow_requestbase o2 with(nolock),workflow_nodebase o3 with(nolock)"+
                    " where o1.requestid=o2.requestid and o2.currentnodeid=o3.id  and o1.requestid in ("+requestids+")"+
                    " union "+
                    " select    o1.requestid,o1.bh,dbo.convToCN(o2.status) status,o3.nodename  from formtable_main_518  o1 with(nolock),workflow_requestbase o2 with(nolock),workflow_nodebase o3 with(nolock)"+
                    " where o1.requestid=o2.requestid and o2.currentnodeid=o3.id  and o1.requestid in ("+requestids+")"+
                    " )t order by requestid";

        rs.executeSql(sql);
        while(rs.next()){

            String requestid=Util.null2String(rs.getString("REQUESTID"));
            String bh=Util.null2String(rs.getString("BH"));
            String status=Util.null2String(rs.getString("STATUS"));
            String nodename=Util.null2String(rs.getString("NODENAME"));

            FcOaClass ee=new FcOaClass();
            ee.setREQUESTID(requestid);
            ee.setBH(bh);
            ee.setSTATUS(status);
            ee.setNODENAME(nodename);
            main.add(ee);
        }

        RecordSet rs1 = new RecordSet();
        String sql1 =
                    " select * from ("+
                    " SELECT  requestid,id,料号 LH,规格 GG,SN,真空物料类别 ZKWLLB,异常物料 YCWL,使用时间 SYSJ,数量 SL,供应商名称 GYSMC,宕机时间 DJSJ,误工工时 WGGS,物品名称 WPMC,SY物料类别 SYWLLB,SY中类 SYZL,SY使用时间  SYSYSJ FROM ("+
                    " select o2.requestid,o1.id,o1.mainid,o1.lh 料号,o1.gg 规格,o1.sn,o1.zkwllb 真空物料类别,o1.yichang 异常物料,o1.zksysjh 使用时间,o1.SL 数量,o1.GYSMC 供应商名称,o1.zkdjsjh 宕机时间,o1.wggs 误工工时,o1.pm  物品名称 ,"+
                    " o1.erp_leibie SY物料类别,o1.erp_zhonglei SY中类,o1.sysj SY使用时间"+
                    " from formtable_main_50_dt4  o1,formtable_main_50 o2 where o1.mainid=o2.id"+
                    " and o2.requestid in ("+requestids+")"+
                    " )O1"+
                    " union "+
                    " SELECT  requestid,id,料号 LH,规格 GG,SN,真空物料类别 ZKWLLB,异常物料 YCWL,使用时间 SYSJ,数量 SL,供应商名称 GYSMC,宕机时间 DJSJ,误工工时 WGGS,物品名称 WPMC,SY物料类别 SYWLLB,SY中类 SYZL,SY使用时间  SYSYSJ FROM ("+
                    " select o2.requestid,o1.id,o1.mainid,o1.lh 料号,o1.gg 规格,o1.sn,o1.zkwllb 真空物料类别,o1.yichang 异常物料,o1.zksysjh 使用时间,o1.SL 数量,o1.GYSMC 供应商名称,o1.zkdjsjh 宕机时间,o1.wggs 误工工时,o1.pm  物品名称 ,"+
                    " o1.erp_leibie SY物料类别,o1.erp_zhonglei SY中类,o1.sysj SY使用时间"+
                    " from formtable_main_518_dt4  o1,formtable_main_518 o2 where o1.mainid=o2.id"+
                    " and o2.requestid in ("+requestids+")"+
                    " )O1  "+
                    " )t order by requestid,ID";

        rs1.executeSql(sql1);
        while(rs1.next()){

            String requestid=Util.null2String(rs1.getString("REQUESTID"));
            String LH=Util.null2String(rs1.getString("LH"));
            String GG=Util.null2String(rs1.getString("GG"));
            String SN=Util.null2String(rs1.getString("SN"));
            String ZKWLLB=Util.null2String(rs1.getString("ZKWLLB"));
            String YCWL=Util.null2String(rs1.getString("YCWL"));
            String SYSJ=Util.null2String(rs1.getString("SYSJ"));
            String SL=Util.null2String(rs1.getString("SL"));
            String GYSMC=Util.null2String(rs1.getString("GYSMC"));
            String DJSJ=Util.null2String(rs1.getString("DJSJ"));
            String WGGS=Util.null2String(rs1.getString("WGGS"));
            String WPMC=Util.null2String(rs1.getString("WPMC"));
            String SYWLLB=Util.null2String(rs1.getString("SYWLLB"));
            String SYZL=Util.null2String(rs1.getString("SYZL"));
            String SYSYSJ=Util.null2String(rs1.getString("SYSYSJ"));

            FcOaClass ee=new FcOaClass();
            ee.setREQUESTID(requestid);
            ee.setLH(LH);
            ee.setGG(GG);
            ee.setSN(SN);
            ee.setZKWLLB(ZKWLLB);
            ee.setYCWL(YCWL);
            ee.setSYSJ(SYSJ);
            ee.setSL(SL);
            ee.setGYSMC(GYSMC);
            ee.setDJSJ(DJSJ);
            ee.setWGGS(WGGS);
            ee.setWPMC(WPMC);
            ee.setSYWLLB(SYWLLB);
            ee.setSYZL(SYZL);
            ee.setSYSYSJ(SYSYSJ);
            dt1s.add(ee);
        }


        RecordSet rs2 = new RecordSet();
        String sql2 =

        " select * from ("+
        " select requestid,id,临时对策 DC,责任部门 ZRBM,责任人 ZRR,完成确认 WCQR,完成日期  WCRQ        from ("+
        " select o2.requestid,o1.id,o1.mainid,o1.lsdc 临时对策,o1.zrbm 责任部门,o1.zrr 责任人,o1.wcqr 完成确认,case when isnull(o1.wcrq,'          ')='          ' then null else cast ( (o1.wcrq+' '+o1.wcsj) as datetime) end 完成日期"+
        " from formtable_main_50_dt1  o1,formtable_main_50 o2 where o1.mainid=o2.id"+
        " and o2.requestid in ("+requestids+")"+
        " )oo"+
        " union "+
        " select requestid,id,临时对策 dc,责任部门 zrbm,责任人 zrr,完成确认 wcqr,完成日期  wcrq        from ("+
        " select o2.requestid,o1.id,o1.mainid,o1.lsdc 临时对策,o1.zrbm 责任部门,o1.zrr 责任人,o1.wcqr 完成确认,case when isnull(o1.wcrq,'          ')='          ' then null else cast ( (o1.wcrq+' '+o1.wcsj) as datetime) end 完成日期"+
        " from formtable_main_518_dt1  o1,formtable_main_518 o2 where o1.mainid=o2.id"+
        " and o2.requestid in ("+requestids+")"+
        " )oo   "+
        " )t order by requestid,ID";

        rs2.executeSql(sql2);
        while(rs2.next()){

            String requestid=Util.null2String(rs2.getString("REQUESTID"));
            String DC=Util.null2String(rs2.getString("DC"));
            String ZRR=Util.null2String(rs2.getString("ZRR"));
            String WCQR=Util.null2String(rs2.getString("WCQR"));
            String WCRQ=Util.null2String(rs2.getString("WCRQ"));

            FcOaClass ee=new FcOaClass();
            ee.setREQUESTID(requestid);
            ee.setDC(DC);
            ee.setZRR(ZRR);
            ee.setWCQR(WCQR);
            ee.setWCRQ(WCRQ);
            dt2s.add(ee);
        }
        BaseBean bb = new BaseBean();

        bb.writeLog("====api20240715===="+ main.size()+"|||"+ dt1s.size()+"|||"+ dt2s.size());

        for(int ss=0;ss<dt2s.size();ss++){
            bb.writeLog("dt2s:"+ dt2s.get(ss).getDC()+"|||"+ dt2s.get(ss).getZRR()+"|||"+ dt2s.get(ss).getREQUESTID());
        }
        bb.writeLog(sql1);
        bb.writeLog(sql2);


        Map result = new HashMap();
        String mainstring="";
        int start_dt1=0;
        int start_dt2=0;

        for( int i=0;i<main.size();i++){
            String requestid=main.get(i).getREQUESTID();
            String bh=main.get(i).getBH();
            String status=main.get(i).getSTATUS();
            String nodename=main.get(i).getNODENAME();

            mainstring="{";
            mainstring+="\"requestid\":\""+requestid+"\"";
            mainstring+=",\"bh\":\""+bh+"\"";
            mainstring+=",\"status\":\""+status+"\"";
            mainstring+=",\"nodename\":\""+nodename+"\"";

            mainstring+=",\"dt1\":[";

            String dt1string="";
            for( int j=start_dt1;j<dt1s.size();j++){
                if(dt1s.get(j).getREQUESTID().equals(main.get(i).getREQUESTID())){

                    String LH=dt1s.get(j).getLH();
                    String GG=dt1s.get(j).getGG();
                    String SN=dt1s.get(j).getSN();
                    String ZKWLLB=dt1s.get(j).getZKWLLB();
                    String YCWL=dt1s.get(j).getYCWL();

                    String SYSJ=dt1s.get(j).getSYSJ();
                    String SL=dt1s.get(j).getSL();
                    String GYSMC=dt1s.get(j).getGYSMC();
                    String DJSJ=dt1s.get(j).getDJSJ();
                    String WGGS=dt1s.get(j).getWGGS();

                    String WPMC=dt1s.get(j).getWPMC();
                    String SYWLLB=dt1s.get(j).getSYWLLB();
                    String SYZL=dt1s.get(j).getSYZL();
                    String SYSYSJ=dt1s.get(j).getSYSYSJ();



                    if(!"".equals(dt1string)){dt1string+=",";}
                    dt1string+="{";
                    dt1string+="\"LH\":\""+LH+"\"";
                    dt1string+=",\"GG\":\""+GG+"\"";
                    dt1string+=",\"SN\":\""+SN+"\"";

                    dt1string+=",\"ZKWLLB\":\""+ZKWLLB+"\"";
                    dt1string+=",\"YCWL\":\""+YCWL+"\"";
                    dt1string+=",\"SYSJ\":\""+SYSJ+"\"";
                    dt1string+=",\"SL\":\""+SL+"\"";
                    dt1string+=",\"GYSMC\":\""+GYSMC+"\"";
                    dt1string+=",\"DJSJ\":\""+DJSJ+"\"";
                    dt1string+=",\"WGGS\":\""+WGGS+"\"";
                    dt1string+=",\"WPMC\":\""+WPMC+"\"";
                    dt1string+=",\"SYWLLB\":\""+SYWLLB+"\"";
                    dt1string+=",\"SYZL\":\""+SYZL+"\"";
                    dt1string+=",\"SYSYSJ\":\""+SYSYSJ+"\"";
                    dt1string+="}";
                }else{
                    start_dt1=j;
                    break;
                }
            }
            mainstring+=dt1string;
            mainstring+="]";

            mainstring+=",\"dt2\":[";


            String dt2string="";
            for( int j=start_dt2;j<dt2s.size();j++){
                if(dt2s.get(j).getREQUESTID().equals(main.get(i).getREQUESTID())){

                    String DC=dt2s.get(j).getDC();
                    String ZRR=dt2s.get(j).getZRR();
                    String WCQR=dt2s.get(j).getWCQR();
                    String WCRQ=dt2s.get(j).getWCRQ();

                    if(!"".equals(dt2string)){dt2string+=",";}
                    dt2string+="{";
                    dt2string+="\"DC\":\""+DC+"\"";
                    dt2string+=",\"ZRR\":\""+ZRR+"\"";
                    dt2string+=",\"WCQR\":\""+WCQR+"\"";
                    dt2string+=",\"WCRQ\":\""+WCRQ+"\"";
                    //dt2string+="\"DC\":\""+DC+"\",\"ZRR\":\""+ZRR+"\",\"WCQR\":\""+WCQR+"\",\"WCRQ\":\""+WCRQ+"\"";

                    dt2string+="}";

                }else{
                    start_dt2=j;
                    break;
                }
            }
            mainstring+=dt2string;
            mainstring+="]";


            mainstring+="}";


            if(!"[".equals(endString)){
                endString+=",";
            }
            endString+=mainstring;
        }
        endString+="]";//1


        Map<String, Object> data = new HashMap<String, Object>();
        data.put("result",endString);
        String json = JSONObject.toJSONString(data);
        return json;
    }

    @GET
    @Path("/get_mains")
    @Produces(MediaType.APPLICATION_JSON)
    public String get_mains(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject errorobj = new JSONObject();
        RecordSet rs = new RecordSet();

        String requestids=request.getParameter("requestid");
        String danhaos=request.getParameter("bh");
        danhaos=danhaos.replaceAll(",","','");

        String endString="[";

        String sql = "select * from fc_zk_kesu_api    where 1=1  ";

        if(!"".equals(requestids)){sql+=" and requestid in ('"+requestids+"')";}

        if(!"".equals(danhaos)){sql+=" and bh in ('"+danhaos+"')";}


        rs.executeSql(sql);
        while(rs.next()){

            String  requestid= Util.null2String(rs.getString("REQUESTID"));

            String  bh= Util.null2String(rs.getString("BH"));
            String  sxrq= Util.null2String(rs.getString("SXRQ"));
            String  kdrq= Util.null2String(rs.getString("KDRQ"));
            String  kklb= Util.null2String(rs.getString("KKLB"));
            String  kkyy= Util.null2String(rs.getString("KKYY"));
            String  pm= Util.null2String(rs.getString("PM"));
            String  ph= Util.null2String(rs.getString("PH"));
            String  blsl= Util.null2String(rs.getString("BLSL"));
            String  kkje= Util.null2String(rs.getString("KKJE"));
            String  nodename= Util.null2String(rs.getString("NODENAME"));
            String  status= Util.null2String(rs.getString("STATUS"));
            String  text_no= Util.null2String(rs.getString("text_no"));




String line="{";
line+="\"requestid\":\""+requestid+"\"";
line+=",\"bh\":\""+bh+"\"";
line+=",\"text_no\":\""+text_no+"\"";
line+=",\"sxrq\":\""+sxrq+"\"";
line+=",\"kdrq\":\""+kdrq+"\"";
line+=",\"kklb\":\""+kklb+"\"";
line+=",\"kkyy\":\""+kkyy+"\"";
line+=",\"pm\":\""+pm+"\"";
line+=",\"ph\":\""+ph+"\"";
line+=",\"blsl\":\""+blsl+"\"";
line+=",\"kkje\":\""+kkje+"\"";
line+=",\"nodename\":\""+nodename+"\"";
line+=",\"status\":\""+status+"\"";


line+="}";

            if(endString.equals("[")){
                endString+=line;
            }else{
                endString+=","+line;
            }
        }
        endString=endString+']';


        Map<String, Object> data = new HashMap<String, Object>();
        data.put("result",endString);
        String json = JSONObject.toJSONString(data);
        return json;
    }






}